//
//  ShoppingCollectionViewCell.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/20.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "CommodityCollectionViewCell.h"
#import "UIImageView+Webcache.h"

@interface CommodityCollectionViewCell()

@property (strong, nonatomic) UIImageView *pictureImageView;
@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UIView *bottomView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UIImageView *purchaseImageView;

@end

@implementation CommodityCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
        self.bgView.backgroundColor = I_COLOR_WHITE;
        [self.contentView addSubview:self.bgView];
        self.bgView.clipsToBounds = YES;
        self.bgView.layer.borderColor = I_COLOR_DIVIDER.CGColor;
        self.bgView.layer.borderWidth = 1.0f;
        
        self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, TRANS_VALUE(124.0f), frame.size.width, frame.size.height - TRANS_VALUE(42.0f))];
        self.bottomView.backgroundColor = I_COLOR_BACKGROUND;
        [self.bgView addSubview:self.bottomView];
        
        self.pictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(20.0f), frame.size.width - 2 * TRANS_VALUE(10.0f), TRANS_VALUE(84.0f))];
        self.pictureImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.pictureImageView.clipsToBounds = YES;
        self.pictureImageView.layer.cornerRadius = TRANS_VALUE(4.0f);
        [self.bgView addSubview:self.pictureImageView];
        self.pictureImageView.image = [UIImage imageNamed:@"ic_commodity_default"];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(10.0f), TRANS_VALUE(140.0f), TRANS_VALUE(22.0f))];
        self.titleLabel.textColor = I_COLOR_BLACK;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
        [self.bottomView addSubview:self.titleLabel];
        self.titleLabel.text = @"[油卡]中石化油卡";
        
        self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(34.0f), TRANS_VALUE(100.0f), TRANS_VALUE(33.0f))];
        self.priceLabel.textColor = I_COLOR_RED;
        self.priceLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
        [self.bottomView addSubview:self.priceLabel];
        self.priceLabel.text = @"￥3000";
        self.priceLabel.hidden = YES;
        
        self.purchaseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(114.0f), TRANS_VALUE(10.0f), TRANS_VALUE(28.0f), TRANS_VALUE(22.0f))];
        self.purchaseImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.purchaseImageView.clipsToBounds = YES;
        self.purchaseImageView.layer.cornerRadius = TRANS_VALUE(2.0f);
        [self.bottomView addSubview:self.purchaseImageView];
        self.purchaseImageView.image = [UIImage imageNamed:@"ic_shopping_purchase"];
    }
    self.contentView.backgroundColor = I_COLOR_GRAY;
    
    return self;
}

- (void)setCommodityInfo:(CommodityInfo *)commodityInfo {
    _commodityInfo = commodityInfo;
    if(_commodityInfo != nil) {
        NSString *titleStr = _commodityInfo.productName != nil ? _commodityInfo.productName : @"";
//        NSString *typeStr = _commodityInfo.typeName != nil ? _commodityInfo.typeName : @"";
//        NSString *title = [typeStr isEqualToString:@""] ? [NSString stringWithFormat:@"%@", titleStr] : [NSString stringWithFormat:@"[%@]%@", typeStr, titleStr];
        self.titleLabel.text = titleStr;
        if(_commodityInfo.imageUrl) {
            NSString *imageURL = [NSString stringWithFormat:@"%@", _commodityInfo.imageUrl];
            [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"ic_commodity_default"]];
        }
        NSString *priceStr = _commodityInfo.price;
        self.priceLabel.text = [NSString stringWithFormat:@"￥%@", priceStr];
    }
}

@end
